---@class LuaObjectMgr : Singleton
---@field protected mBaseView IBaseView[]
local this = BaseClass('LuaObjectMgr', Singleton)

---@return IMediator
function this:NewCtrlScript(id)
    local item = TableDataMgr.UICfgMgr:GetItemByKey(id)
    if(not item)then return item; end

    local ctrl = require(item.ScriptCtrl)
    local view = self:NewViewScript(item.LuaObjId)
    return ctrl(view)
end

---@return IBaseView
function this:NewViewScript(id)
    local item = TableDataMgr.LuaObjectMgr:GetItemByKey(id)
    if(not item)then return item; end

    self.mUniqueId = self.mUniqueId + 1
    ---@type IBehaviour
    local panel = Utility.NewLuaScript(item.ScriptPanel)

    ---@type IBaseView
    local view = Utility.NewLuaScript(item.ScriptView)
    if(view and panel)then
        view:Init({self.mUniqueId, panel})
    end
    return view
end

---@param eType string|number
---@param param userdata
function this:PushLuaEvent(eType, param)
    LuaHelper.DispatchEvent(eType, param)
end

---@param luaView IBaseView
function this:AddLuaView(luaView)
    table.insert(self.mBaseView, luaView)
end

function this:RemoveLuaView(luaView)
    for key, value in pairs(self.mBaseView) do
        if (value == luaView) then
            self.mBaseView[key] = nil
        end
    end
end

function this:Awake()
    self.mUniqueId = 0
    self.mBaseView = {}

    TableDataMgr:Init()
    PlayerData:Init()
    FaceCenter:Awake()
end

function this:Update()
    for key, value in pairs(self.mBaseView) do
        if (value and value:IsUpdate()) then
            if (value.Update) then
                value:Update()
            end
        end
    end
end

function this:FixedUpdate()
    for key, value in pairs(self.mBaseView) do
        if (value and value:IsUpdate()) then
            if (value.FixedUpdate) then
                value:FixedUpdate()
            end
        end
    end
end

function this:LateUpdate()
    for key, value in pairs(self.mBaseView) do
        if (value and value:IsUpdate()) then
            if (value.LateUpdate) then
                value:LateUpdate()
            end
        end
    end
end

function this:Destroy()
    self.mUniqueId = nil
    self.mBaseView = nil
    FaceCenter:Destroy()
end

---@return LuaObjectMgr
LuaObjMgr = this:GetInstance()